package com.sect.edu.info.manager.service;

import com.sect.edu.info.manager.dao.StudentDao;
import com.sect.edu.info.manager.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author げっしょく
 * @date 2021/7/17
 */
public class StudentService {
        //创建一个StudentDao的对象
    StudentDao studen = new StudentDao();
        //使用创建的的对象调用其添加学生的对象并返回其结果
    public boolean addStudent(Student stu) {
        return studen.addStudent(stu);
    }


    public boolean isExists(String id) {
        ArrayList<Student> list = studen.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu1 = list.get(i);
            if(id.equals(stu1.getId())){
                return true;
            }
        }
        return false;
    }

    public boolean deleteStuden(String id) {
        ArrayList<Student> list = studen.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu1 = list.get(i);
            if(id.equals(stu1.getId())){
                list.remove(i);
                return true;
            }
        }
        return false;
    }

    public void setData(Student stu) {
        ArrayList<Student> list = studen.findAllStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu1 = list.get(i);
            if(stu.getId().equals(stu1.getId())){
                list.set(i , stu);
                return ;
            }
        }
    }

    public ArrayList<Student> rtlArray() {
        return studen.findAllStudent();
    }
}


/*
    public boolean isExists(String id) {
        Student[] stus = studen.findAllStudent();
        for (int i = 0; i < stus.length; i++) {
            Student stu1 = stus[i];
            if (stu1 != null && id == stu1.getId()) {
                return true;
            }
        }
        return false;
    }

    public boolean deleteStuden(String id) {
        Student[] stu2 = studen.findAllStudent();
        for (int i = 0; i < stu2.length; i++) {
            Student stu3 = stu2[i];
            if(stu3 != null && id.equals(stu3.getId())){
                if(i  == stu2.length - 1){
                    stu2[i] = null;
                    return true;
                }
                for ( ; i < stu2.length; i++) {
                    if(i + 1 == stu2.length - 1){
                        stu2[i] = stu2[i + 1];
                        stu2[i+1] = null;
                        return true;
                    }
                }
            }
        }
        return false;
    }
 */




//*******************************************************************
/*
    public String sameId(Student[] stu,String tempid){
        Scanner sc = new Scanner(System.in);
        String id = "";
        for (int i = 0; i < stu.length; i++) {
            id = stu[i].getId();
            if(id.equals("null")){
                return tempid;
            }else if(id.equals(tempid)){
                System.out.println("你输入的学号已存在，请重新输入");
                tempid = sc.next();
                i = -1;
            }
        }
        return tempid;
    }
    public void ScannerStudent(Student[] stu , int i){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = sc.next();
        stu[i].setName(name);        System.out.println("请输入学生学号");
        String id = sameId(stu , sc.next());
//        stu[i].setName(id);
        System.out.println("请输入学生生日");
        stu[i].setName(sc.next());
        System.out.println("请输入学生年龄");
        stu[i].setAge(sc.nextInt());
    }
    public void add(Student[] stu) {
        int i = 0;
        for (i = 0; i < stu.length; i++) {
            if(stu[i]==null){
                break;
            }
        }
        ScannerStudent(stu,i);

    }
 */